<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/6
 * Time: 17:44
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Goodscate extends Base
{
    /**
     * 类目列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $keyword = input('keyword');
            $where['gt_del'] = 1;
            if(isset($keyword) && $keyword != ''){
                $where['gt_name'] = ['like', '%' . $keyword . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods_type')->where($where)->count();// 获取总条数
            $lists = Db::name('goods_type')
                ->where($where)
                ->field('gt_id, gt_thumb, gt_name, gt_time, gt_updatetime')
                ->page($Nowpage,$limits)
                ->order('gt_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加分类
     * @return mixed|\think\response\Json
     */
    public function cateAdd(){
        if(Request::instance()->isPost()){
            $gt_name = input('gt_name');
            $gt_thumb = input('gt_thumb');
            $insert = [
                'gt_thumb' => $gt_thumb,
                'gt_name' => $gt_name,
                'gt_time' => date('Y-m-d H:i:s'),
                'gt_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_goods_type')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('goodscate/cateadd');
    }
    
    /**
     * 编辑分类
     * @return mixed|\think\response\Json
     */
    public function cateEdit(){
        $gt_id = input('gt_id');
        if(Request::instance()->isPost()){
            $gt_name = input('gt_name');
            $gt_thumb = input('gt_thumb');
            $update = [
                'gt_name' => $gt_name,
                'gt_updatetime' => date('Y-m-d H:i:s')
            ];
            if(!empty($gt_thumb)){
                $update['gt_thumb'] = $gt_thumb;
            }
            $res = Db::table('up_goods_type')->where('gt_id', $gt_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cate = Db::table('up_goods_type')->field('gt_name, gt_thumb')->where('gt_id', $gt_id)->find();
        return $this->fetch('goodscate/cateedit', ['cate' => $cate, 'gt_id' => $gt_id]);
    }
    
    /**
     * 删除分类
     * @return \think\response\Json
     */
    public function cateDel(){
        $gt_id = input('id');
        $res = Db::table('up_goods_type')->where('gt_id', $gt_id)->setField('gt_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}